package com.qf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qf.Resp.BaseResp;
import com.qf.classes.Clsses;
import com.qf.client.ClassesClient;
import com.qf.dao.UserRespository;
import com.qf.dao.UserRespository2;
import com.qf.service.UserService2;
import com.qf.user.TbUser;
import com.qf.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author H
 * @create 2020-11-12 16:19
 * @strive To be whoever you want to be.
 * @Description:
 */
@Service
public class UserServiceImpl2 implements UserService2 {


    @Autowired
    UserRespository2 userRespository2;

    @Autowired
    JWTUtils jwtUtils;

    @Override
    public BaseResp login2(TbUser tbUser) {
        //声明返回BaseResp
        BaseResp baseResp = new BaseResp();
        if (tbUser.getLoginName() == null || tbUser.getLoginName().equals("") || tbUser.getPassword() == null || tbUser.getPassword().equals("")) {
            baseResp.setCode(100001l);
            baseResp.setMessage("信息输入不完整");
            return baseResp;
        }

        TbUser byLoginNameAndPassword = userRespository2.findByLoginNameAndPassword(tbUser.getLoginName(), tbUser.getPassword());

        if (byLoginNameAndPassword == null) {
            baseResp.setCode(100002l);
            baseResp.setMessage("用户信息输入错误，请重新输入");
            return baseResp;
        }
        Map o = (Map) JSONObject.toJSON(byLoginNameAndPassword);

        String token = jwtUtils.token(o);

        baseResp.setCode(100000l);
        baseResp.setMessage(token);
        return baseResp;
    }


}
